package com.noname.lib_base_java.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2018-09-21 09:58
 * 文件名: CommonAdapter
 * 描述:RecyclerView适配器基类
 */
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    protected String TAG = this.getClass().getSimpleName();
    protected LayoutInflater mInflater;
    protected List<T> mData;
    protected Context mContext;
    private int mLayoutId = -1;
    protected MultipleType<T> mTypeSupport;

    public CommonAdapter(Context mContext, List<T> mData) {
        this.mData = mData;
        this.mLayoutId = -1;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public CommonAdapter(Context mContext, List<T> mData, int mLayoutId) {
        this.mData = mData;
        this.mLayoutId = mLayoutId;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public CommonAdapter(Context mContext, List<T> mData, MultipleType<T> mTypeSupport) {
        this.mData = mData;
        this.mLayoutId = mLayoutId;
        this.mContext = mContext;
        this.mTypeSupport = mTypeSupport;
        mInflater = LayoutInflater.from(mContext);

    }


    public void clearData() {
        mData.clear();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mTypeSupport != null) {
            mLayoutId = viewType;
        }
        View view = mInflater.inflate(mLayoutId, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        if (mTypeSupport != null) {
            return mTypeSupport.getLayoutId(mData.get(position), position);
        } else {
            return super.getItemViewType(position);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        bindData(holder, position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    protected abstract void bindData(ViewHolder holder, int position);
}
